/*
 * @(#)SysUserDefinedIndex.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.bean.SysUserDefinedIndex;
import com.dcivision.framework.bean.SysUserDefinedIndexDetail;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.framework.web.ListSysUserDefinedIndexForm;

/**
  SysUserDefinedIndexDAObject.java

  This class is the data access bean for table "SYS_USER_DEFINED_INDEX".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   15/10/2003
  @version     $Revision: 1.30.2.1 $
*/

public class SysUserDefinedIndexDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.30.2.1 $";

  public static final String TABLE_NAME = "SYS_USER_DEFINED_INDEX";

  public SysUserDefinedIndexDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("PARENT_ID");
    this.vecDBColumn.add("OBJECT_TYPE");
    this.vecDBColumn.add("USER_DEFINED_TYPE");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("GEN_KEY_TEMPLATE");
    this.vecDBColumn.add("GEN_KEY_NUM_MAX");
    this.vecDBColumn.add("IMPORT_TYPE");
    this.vecDBColumn.add("WORKFLOW_RECORD_ID");
    this.vecDBColumn.add("DMS_PARENT_ID");
    this.vecDBColumn.add("DMS_ROOT_ID");
    this.vecDBColumn.add("DMS_SCAN_FOLDER_CREATION_TYPE");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));

          // Retrieve the scan folder path name
          if (!Utility.isEmpty(tmpSysUserDefinedIndex.getDmsRootID()) && !Utility.isEmpty(tmpSysUserDefinedIndex.getDmsParentID())) {
            try {
              DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(this.sessionContainer, this.dbConn);
              tmpSysUserDefinedIndex.setDmsScanFolderPath(docRetrievalManager.getLocationPath(tmpSysUserDefinedIndex.getDmsParentID()));
            } catch (Exception ignore) {
              log.error(ignore, ignore);
            }
          }

          return(tmpSysUserDefinedIndex);
        } else {
          return null;
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    boolean showSystemDefinedIndex = true;

    if(searchForm instanceof ListSysUserDefinedIndexForm){
      showSystemDefinedIndex = ((ListSysUserDefinedIndexForm)searchForm).getUseSysDefinedIndex();
    }

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        //determine to get the negative system value or not
        if(showSystemDefinedIndex==false){
          sqlStat.append("AND A.ID >= 0 ");
        }

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
					String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          tmpSysUserDefinedIndex.setRecordCount(totalNumOfRecord);
          tmpSysUserDefinedIndex.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpSysUserDefinedIndex);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.USER_DEFINED_TYPE ASC ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          result.add(tmpSysUserDefinedIndex);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
    SysUserDefinedIndex sysUserDefinedIndex = (SysUserDefinedIndex)obj;
    StringBuffer sbSqlStat = new StringBuffer();
    PreparedStatement pstat = null;
    ResultSet rs = null;

    // Check duplicate index name with the same object type.
    sbSqlStat.append("SELECT COUNT(*) ");
    sbSqlStat.append("FROM   SYS_USER_DEFINED_INDEX ");
    sbSqlStat.append("WHERE  OBJECT_TYPE = ? AND USER_DEFINED_TYPE = ? AND RECORD_STATUS = ? ");

    try {
      pstat = this.dbConn.prepareStatement(sbSqlStat.toString());
      this.setPrepareStatement(pstat, 1, sysUserDefinedIndex.getObjectType());
      this.setPrepareStatement(pstat, 2, sysUserDefinedIndex.getUserDefinedType());
      this.setPrepareStatement(pstat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
      rs = pstat.executeQuery();
      if (rs.next() && rs.getInt(1)>0) {
        String errorCode = ErrorConstant.SYS_DUPLICATE_INDEX_NAME;
        if (GlobalConstant.OBJECT_TYPE_DOCUMENT.equals(sysUserDefinedIndex.getObjectType())) {
          errorCode = com.dcivision.dms.DmsErrorConstant.DUPLICATE_PROFILE_NAME;
        }
        throw new ApplicationException(errorCode);
      }
    } catch (java.sql.SQLException sqle) {
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { pstat.close(); } catch (Exception ignore) {} finally { pstat = null; }
    }

  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
    SysUserDefinedIndex sysUserDefinedIndex = (SysUserDefinedIndex)obj;
     StringBuffer sbSqlStat = new StringBuffer();
     PreparedStatement pstat = null;
     ResultSet rs = null;

     // Check duplicate index name with the same object type.
     sbSqlStat.append("SELECT COUNT(*) ");
     sbSqlStat.append("FROM   SYS_USER_DEFINED_INDEX ");
     sbSqlStat.append("WHERE  OBJECT_TYPE = ? AND USER_DEFINED_TYPE = ? AND RECORD_STATUS = ? AND ID <> ? ");

     try {
       pstat = this.dbConn.prepareStatement(sbSqlStat.toString());
       this.setPrepareStatement(pstat, 1, sysUserDefinedIndex.getObjectType());
       this.setPrepareStatement(pstat, 2, sysUserDefinedIndex.getUserDefinedType());
       this.setPrepareStatement(pstat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
       this.setPrepareStatement(pstat, 4, sysUserDefinedIndex.getID());
       rs = pstat.executeQuery();
       if (rs.next() && rs.getInt(1)>0) {
         String errorCode = ErrorConstant.SYS_DUPLICATE_INDEX_NAME;
         if (GlobalConstant.OBJECT_TYPE_DOCUMENT.equals(sysUserDefinedIndex.getObjectType())) {
           errorCode = com.dcivision.dms.DmsErrorConstant.DUPLICATE_PROFILE_NAME;
         }
         throw new ApplicationException(errorCode);
       }
     } catch (java.sql.SQLException sqle) {
       throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
     } finally {
       try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
       try { pstat.close(); } catch (Exception ignore) {} finally { pstat = null; }
     }

  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = (SysUserDefinedIndex)((SysUserDefinedIndex)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   SYS_USER_DEFINED_INDEX(ID, PARENT_ID, OBJECT_TYPE, USER_DEFINED_TYPE, DESCRIPTION, GEN_KEY_TEMPLATE, GEN_KEY_NUM_MAX, IMPORT_TYPE, WORKFLOW_RECORD_ID, DMS_PARENT_ID, DMS_ROOT_ID, DMS_SCAN_FOLDER_CREATION_TYPE, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpSysUserDefinedIndex.getParentID());
        setPrepareStatement(preStat, 3, tmpSysUserDefinedIndex.getObjectType());
        setPrepareStatement(preStat, 4, tmpSysUserDefinedIndex.getUserDefinedType());
        setPrepareStatement(preStat, 5, tmpSysUserDefinedIndex.getDescription());
        setPrepareStatement(preStat, 6, tmpSysUserDefinedIndex.getGenKeyTemplate());
        setPrepareStatement(preStat, 7, tmpSysUserDefinedIndex.getGenKeyNumMax());
        setPrepareStatement(preStat, 8, tmpSysUserDefinedIndex.getImportType());
        setPrepareStatement(preStat, 9, tmpSysUserDefinedIndex.getWorkflowRecordID());

        setPrepareStatement(preStat, 10, tmpSysUserDefinedIndex.getDmsParentID());
        setPrepareStatement(preStat, 11, tmpSysUserDefinedIndex.getDmsRootID());
        setPrepareStatement(preStat, 12, tmpSysUserDefinedIndex.getDmsScanFolderCreationType());

        setPrepareStatement(preStat, 13, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 14, new Integer(0));
        if (sessionContainer!=null &&  sessionContainer.getUserRecordID()!=null) {
          setPrepareStatement(preStat, 15, sessionContainer.getUserRecordID());
        } else {
          setPrepareStatement(preStat, 15, tmpSysUserDefinedIndex.getCreatorID());
        }
        setPrepareStatement(preStat, 16, currTime);
        if (sessionContainer!=null &&  sessionContainer.getUserRecordID()!=null) {
          setPrepareStatement(preStat, 17, sessionContainer.getUserRecordID());
        } else {
          setPrepareStatement(preStat, 17, tmpSysUserDefinedIndex.getCreatorID());
        }
        setPrepareStatement(preStat, 18, currTime);
        preStat.executeUpdate();
        tmpSysUserDefinedIndex.setID(nextID);
        if (sessionContainer!=null &&  sessionContainer.getUserRecordID()!=null) {
          tmpSysUserDefinedIndex.setCreatorID(sessionContainer.getUserRecordID());
        } else {
          tmpSysUserDefinedIndex.setCreatorID(tmpSysUserDefinedIndex.getCreatorID());
        }
        tmpSysUserDefinedIndex.setCreateDate(currTime);
        if (sessionContainer!=null &&  sessionContainer.getUserRecordID()!=null) {
          tmpSysUserDefinedIndex.setUpdaterID(sessionContainer.getUserRecordID());
        } else {
          tmpSysUserDefinedIndex.setUpdaterID(tmpSysUserDefinedIndex.getCreatorID());
        }

        tmpSysUserDefinedIndex.setUpdateDate(currTime);
        tmpSysUserDefinedIndex.setUpdateCount(new Integer(0));
        tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
        tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
        return(tmpSysUserDefinedIndex);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = (SysUserDefinedIndex)((SysUserDefinedIndex)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE SYS_USER_DEFINED_INDEX ");
        sqlStat.append("SET  PARENT_ID=?, OBJECT_TYPE=?, USER_DEFINED_TYPE=?, DESCRIPTION=?, GEN_KEY_TEMPLATE=?, GEN_KEY_NUM_MAX=?, IMPORT_TYPE=?, WORKFLOW_RECORD_ID=?, DMS_PARENT_ID=?, DMS_ROOT_ID=?, DMS_SCAN_FOLDER_CREATION_TYPE=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpSysUserDefinedIndex.getParentID());
        setPrepareStatement(preStat, 2, tmpSysUserDefinedIndex.getObjectType());
        setPrepareStatement(preStat, 3, tmpSysUserDefinedIndex.getUserDefinedType());
        setPrepareStatement(preStat, 4, tmpSysUserDefinedIndex.getDescription());
        setPrepareStatement(preStat, 5, tmpSysUserDefinedIndex.getGenKeyTemplate());
        setPrepareStatement(preStat, 6, tmpSysUserDefinedIndex.getGenKeyNumMax());
        setPrepareStatement(preStat, 7, tmpSysUserDefinedIndex.getImportType());
        setPrepareStatement(preStat, 8, tmpSysUserDefinedIndex.getWorkflowRecordID());
        setPrepareStatement(preStat, 9, tmpSysUserDefinedIndex.getDmsParentID());
        setPrepareStatement(preStat, 10, tmpSysUserDefinedIndex.getDmsRootID());
        setPrepareStatement(preStat, 11, tmpSysUserDefinedIndex.getDmsScanFolderCreationType());
        setPrepareStatement(preStat, 12, new Integer(tmpSysUserDefinedIndex.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        setPrepareStatement(preStat, 15, tmpSysUserDefinedIndex.getID());
        setPrepareStatement(preStat, 16, tmpSysUserDefinedIndex.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpSysUserDefinedIndex.setUpdaterID(sessionContainer.getUserRecordID());
          tmpSysUserDefinedIndex.setUpdateDate(currTime);
          tmpSysUserDefinedIndex.setUpdateCount(new Integer(tmpSysUserDefinedIndex.getUpdateCount().intValue() + 1));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          return(tmpSysUserDefinedIndex);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = (SysUserDefinedIndex)((SysUserDefinedIndex)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpSysUserDefinedIndex.getID());
        setPrepareStatement(preStat, 2, tmpSysUserDefinedIndex.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpSysUserDefinedIndex);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    SysUserDefinedIndex tmpSysUserDefinedIndex = (SysUserDefinedIndex)this.oldValue;

    if (tmpSysUserDefinedIndex != null) {
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getParentID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getObjectType()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUserDefinedType()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDescription()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getGenKeyTemplate()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getGenKeyNumMax()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getImportType()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getWorkflowRecordID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsParentID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsRootID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsScanFolderCreationType()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdateDate()));
    }

    tmpSysUserDefinedIndex = (SysUserDefinedIndex)obj;
    if (tmpSysUserDefinedIndex != null) {
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getParentID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getObjectType()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUserDefinedType()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDescription()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getGenKeyTemplate()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getGenKeyNumMax()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getImportType()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getWorkflowRecordID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsParentID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsRootID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getDmsScanFolderCreationType()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpSysUserDefinedIndex.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized List getListByStatus(String status) throws ApplicationException{
    return getListByStatusForWorkflow(status,null);
  }
  public synchronized List getListByStatusForWorkflow(String status,String orderBy) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE A.RECORD_STATUS = ? AND A.ID > 0");
        if(!Utility.isEmpty(orderBy)){
          sqlStat.append(" ORDER BY "+orderBy);
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, status);
        rs = preStat.executeQuery();

        while (rs.next()) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          result.add(tmpSysUserDefinedIndex);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return result;
  }
  public synchronized List getListByObjectTypeStatus(String objType, String status) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE A.OBJECT_TYPE = ? AND A.RECORD_STATUS = ? AND A.ID > 0");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, objType);
        this.setPrepareStatement(preStat, 2, status);
        rs = preStat.executeQuery();

        while (rs.next()) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          result.add(tmpSysUserDefinedIndex);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return result;
  }

  public synchronized List getDetailList(Integer userDefinedIndexID) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT D.ID, D.FIELD_NAME, D.MANDATORY, D.VALUE_SUGGEST, D.FIELD_TYPE, D.RECORD_STATUS, D.UPDATE_COUNT, D.CREATOR_ID, D.CREATE_DATE, D.UPDATER_ID, D.UPDATE_DATE ");
        sqlStat.append("FROM SYS_USER_DEFINED_INDEX H INNER JOIN SYS_USER_DEFINED_INDEX_DETAIL D ON H.ID = D.USER_DEFINED_ID ");
        sqlStat.append("WHERE (D.RECORD_STATUS = 'A') AND D.USER_DEFINED_ID = ?");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, userDefinedIndexID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          SysUserDefinedIndexDetail tmpSysUserDefinedIndexDetail = new SysUserDefinedIndexDetail();
          tmpSysUserDefinedIndexDetail.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndexDetail.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpSysUserDefinedIndexDetail.setMandatory(getResultSetString(rs, "MANDATORY"));
          
          tmpSysUserDefinedIndexDetail.setValueSuggest(getResultSetString(rs, "VALUE_SUGGEST"));
          
          tmpSysUserDefinedIndexDetail.setFieldType(getResultSetString(rs, "FIELD_TYPE"));
          tmpSysUserDefinedIndexDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndexDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndexDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndexDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndexDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndexDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndexDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndexDetail.getCreatorID()));
          tmpSysUserDefinedIndexDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndexDetail.getUpdaterID()));
          result.add(tmpSysUserDefinedIndexDetail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return result;
  }

  public synchronized void deleteByID(String[] id) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM SYS_USER_DEFINED_INDEX ");
        sqlStat.append("WHERE  ID=? ");
        for (int i=1; i<id.length ; i++) {
          sqlStat.append(" OR ID=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        for (int i=0; i<id.length ; i++) {
          setPrepareStatement(preStat, i+1, id[i]);
        }
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized Integer getUDFIDByTypeName(String userDefinedType) throws ApplicationException{
  PreparedStatement preStat = null;
  ResultSet rs = null;
  StringBuffer sqlStat = new StringBuffer();
  List result = new ArrayList();
  synchronized(dbConn) {
    try {
      sqlStat.append("SELECT ID, USER_DEFINED_TYPE ");
      sqlStat.append("FROM SYS_USER_DEFINED_INDEX ");
      sqlStat.append("WHERE RECORD_STATUS = 'A' AND USER_DEFINED_TYPE = ? ");
      preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      setPrepareStatement(preStat, 1, userDefinedType.trim());
      rs = preStat.executeQuery();
      if (rs.next()) {
        return getResultSetInteger(rs, "ID");
      } else {
        return null;
      }
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
    }
  }
  }
  public synchronized boolean isDuplicateTypeName(String userDefinedType) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT USER_DEFINED_TYPE ");
        sqlStat.append("FROM SYS_USER_DEFINED_INDEX ");
        sqlStat.append("WHERE RECORD_STATUS = 'A' AND USER_DEFINED_TYPE = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, userDefinedType.trim());
        rs = preStat.executeQuery();
        if (rs.next()) {
          return true;
        } else {
          return false;
        }
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized Integer getIDByName(String name) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = null;
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE A.USER_DEFINED_TYPE = ? AND A.ID > 0");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, name);
        rs = preStat.executeQuery();

        while (rs.next()) {
          tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return tmpSysUserDefinedIndex.getID();
  }

  public synchronized void updateFieldValue(AbstractBaseObject obj, String targetField, String value) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = (SysUserDefinedIndex)((SysUserDefinedIndex)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE SYS_USER_DEFINED_INDEX ");
        sqlStat.append("SET  "+targetField+"=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, value);
        setPrepareStatement(preStat, 2, new Integer(tmpSysUserDefinedIndex.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 3, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 4, currTime);
        setPrepareStatement(preStat, 5, tmpSysUserDefinedIndex.getID());
        setPrepareStatement(preStat, 6, tmpSysUserDefinedIndex.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized AbstractBaseObject getObjectByUserDefinedType(String name) throws ApplicationException{
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    SysUserDefinedIndex tmpSysUserDefinedIndex = null;
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A ");
        sqlStat.append("WHERE  A.USER_DEFINED_TYPE = ? AND A.ID > 0 AND RECORD_STATUS=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, name);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();

        if (rs.next()) {
          tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
        }
        return tmpSysUserDefinedIndex;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized boolean isRequiredUpdate(java.sql.Timestamp lastUpdateDate) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT COUNT(*) ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX ");
        sqlStat.append("WHERE  UPDATE_DATE > ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        //this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 1, lastUpdateDate);
        rs = preStat.executeQuery();

        if (rs.next() && rs.getInt(1) == 0) {
          return false;
        }
        return true;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized Timestamp getMaxUpdateDate() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT MAX(UPDATE_DATE) AS MAX_UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);


        rs = preStat.executeQuery();

        if (rs.next() ) {
          return getResultSetTimestamp(rs, "MAX_UPDATE_DATE");
        }else{
          return null;
        }

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * get bind profile list by workflow record ID
   * @param workflowRecordID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getBindProfileListByWorkflowRecordID(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.ID, A.PARENT_ID, A.OBJECT_TYPE, A.USER_DEFINED_TYPE, A.DESCRIPTION, A.GEN_KEY_TEMPLATE, A.GEN_KEY_NUM_MAX, A.IMPORT_TYPE, A.WORKFLOW_RECORD_ID, A.DMS_PARENT_ID, A.DMS_ROOT_ID, A.DMS_SCAN_FOLDER_CREATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_USER_DEFINED_INDEX A, MTM_WF_RECORD_OBJECT_LINK B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        sqlStat.append("       AND B.OBJECT_TYPE = ? ");//"U"
        sqlStat.append("       AND A.ID = B.OBJECT_ID ");
        sqlStat.append("       AND B.WORKFLOW_RECORD_ID = ? " );
        sqlStat.append("ORDER BY A.USER_DEFINED_TYPE ASC ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, GlobalConstant.OBJECT_TYPE_UDF);
        this.setPrepareStatement(preStat, 3, workflowRecordID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          SysUserDefinedIndex tmpSysUserDefinedIndex = new SysUserDefinedIndex();
          tmpSysUserDefinedIndex.setID(getResultSetInteger(rs, "ID"));
          tmpSysUserDefinedIndex.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysUserDefinedIndex.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpSysUserDefinedIndex.setUserDefinedType(getResultSetString(rs, "USER_DEFINED_TYPE"));
          tmpSysUserDefinedIndex.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpSysUserDefinedIndex.setGenKeyTemplate(getResultSetString(rs, "GEN_KEY_TEMPLATE"));
          tmpSysUserDefinedIndex.setGenKeyNumMax(getResultSetInteger(rs, "GEN_KEY_NUM_MAX"));
          tmpSysUserDefinedIndex.setImportType(getResultSetString(rs, "IMPORT_TYPE"));
          tmpSysUserDefinedIndex.setWorkflowRecordID(getResultSetInteger(rs, "WORKFLOW_RECORD_ID"));
          tmpSysUserDefinedIndex.setDmsParentID(getResultSetInteger(rs, "DMS_PARENT_ID"));
          tmpSysUserDefinedIndex.setDmsRootID(getResultSetInteger(rs, "DMS_ROOT_ID"));
          tmpSysUserDefinedIndex.setDmsScanFolderCreationType(getResultSetString(rs, "DMS_SCAN_FOLDER_CREATION_TYPE"));
          tmpSysUserDefinedIndex.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpSysUserDefinedIndex.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpSysUserDefinedIndex.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysUserDefinedIndex.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysUserDefinedIndex.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysUserDefinedIndex.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysUserDefinedIndex.setCreatorName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getCreatorID()));
          tmpSysUserDefinedIndex.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysUserDefinedIndex.getUpdaterID()));
          result.add(tmpSysUserDefinedIndex);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
}
